// pages/liveRome/liveRome.js
let livePlayer = requirePlugin('live-player-plugin');// 获取直播间单次订阅状态
Page({

  /**
   * 页面的初始数据
   */
  data: {
    roomId: 17,
    customParams: encodeURIComponent(JSON.stringify({ path: 'pages/index/index', pid: 1 })),
    page: 1,
    pageSize: 10,
    pageTatol:1,
    roomList: [],
    isEnd:false,
    start:0,
    limit:100
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList();
  },
  getList() {
    console.log(131313)
    var key = wx.getStorageSync('loginInfo').userInfo.key
    console.log(key, 'key')
    var that = this;
    console.log(Math.ceil(that.data.pageTatol/that.data.pageSize),'ceil')
    if(that.data.start>that.data.pageTatol){
      that.setData({
        isEnd:true
      })
      return
    }
    // http://localhost:8086/mobile/index.php?act=live_rooms_mobile&op=get_roomslist&key=e988e1f94514ae7c4393b6789f706e0f
    wx.request({
      method: 'get',
      url: `${getApp().globalData.webUrl}/mobile/index.php?act=live_rooms_mobile&op=get_roomslist`,//&page=${this.data.page}&pageSize=${this.data.pageSize}
      data: {
        key: key,
        "start": this.data.start, // 起始拉取房间，start = 0 表示从第 1 个房间开始拉取
        "limit": this.data.limit // 每次拉取的个数上限，不要设置过大，建议 100 以内
      },
      success: function (res) {
        console.log(res,'total')
        that.data.page++;
        if(res.data.datas){
          that.setData({
            start:that.data.start+100,
            pageTatol:res.data.datas.total,
            roomList: that.data.roomList.concat(res.data.datas.room_info)
          })
          console.log(that.data.roomList)
          that.data.roomList.forEach((item,index)=>{
            that.syncLiveStatus(item)
          })
        }else{
          that.setData({
            isEnd:true
          })
        }
       
      }
    })
  },
  syncLiveStatus: function(room) {
    if(room == null){
      return
    }
    let that = this

    let livePlayer = requirePlugin('live-player-plugin') // 引入获取直播状态接口
    // 首次获取立马返回直播状态，往后间隔1分钟或更慢的频率去轮询获取直播状态
    livePlayer.getLiveStatus({ room_id: room.roomid })
    .then(res => {
      // 101: 直播中, 102: 未开始, 103: 已结束, 104: 禁播, 105: 暂停中, 106: 异常, 107：已过期 
      const liveStatus = res.liveStatus
      if(room.live_status != liveStatus){
        let rooms = that.data.rooms
        room.live_status = liveStatus
        console.log(room,'room')
      }
    })
    .catch(err => {
      console.log(err)
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      start:0,
      roomList: [],
    })
    this.getList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})